//定时任务

const schedule = require('node-schedule');
const fs = require('fs');
const path = require('path');
const moment = require('moment');

let file_path = path.join(__dirname , "../../mp3");
schedule.scheduleJob("00 30 03 * * *", function(){
    let timeStr = moment().format("YYYYMMDDHH");
    if(!fs.existsSync(file_path)){
        console.log(file_path , "----不存在")
        return
    }

    let dir = fs.readdirSync(file_path);

    if(!dir || dir.length <= 0 ){
        return
    }
    remove(file_path,dir,timeStr)
})

function remove(file_path,dir,timeStr){

    for(let i = 0 ;  i < dir.length ; i++){
        let name = dir[i];
        let  path_name = path.join(file_path ,"/",name);
        if(name == timeStr){
            continue
        }
        let stat = fs.statSync(path_name)
        if(stat.isFile()){
            fs.unlinkSync(path_name)
            continue
        }
        let subDir = fs.readdirSync(path_name)
        remove(path_name,subDir,timeStr)
        fs.rmdirSync(path_name)
    }
}




